package com.agileboot.domain.store.db;

import cn.hutool.core.lang.Assert;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.medicine.db.Medicine;
import com.agileboot.domain.medicine.dto.MedicineDTO;
import com.agileboot.domain.store.StoreQuery;
import com.agileboot.domain.store.form.StoreMedicineForm;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.ThreadUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 收藏信息表
(StoreInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-02-22 19:22:40
 */
@Service("storeInfoService")
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {

    @Override
    public ResponseDTO saveStoreMedicine(StoreMedicineForm storeMedicineForm) {
        //校验数据
        Assert.isTrue(Objects.nonNull(storeMedicineForm),"参数为空");
        Assert.isTrue(storeMedicineForm.getMedicineId()!=null,"收藏的草药id为空");
        Assert.isTrue(StringUtils.isNotBlank(storeMedicineForm.getMedicineName()),"收藏的草药名称为空");
        //获取上下文中的用户信息
        SystemLoginUser systemLoginUser = AuthenticationUtils.getSystemLoginUser();
        Assert.isTrue(Objects.nonNull(systemLoginUser),"上下文中没有用户信息");
        //判断是收藏还是取消收藏
        //通过用户id和草药id进行排查
        LambdaQueryWrapper<StoreInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreInfo::getMedicineId,storeMedicineForm.getMedicineId());
        wrapper.eq(StoreInfo::getUserId,systemLoginUser.getUserId());
        wrapper.eq(StoreInfo::getDeleted,0);
        Long count = baseMapper.selectCount(wrapper);
        if (count<=0){
            //封装数据
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setMedicineId(storeMedicineForm.getMedicineId());
            storeInfo.setMedicineName(storeMedicineForm.getMedicineName());
            storeInfo.setUserId(systemLoginUser.getUserId());
            storeInfo.setCreateTime(new Date());
            this.save(storeInfo);
            return ResponseDTO.ok("收藏成功");
        }
        baseMapper.delete(wrapper);
        return ResponseDTO.ok("取消收藏");
    }

    @Override
    public ResponseDTO isStore(StoreMedicineForm storeMedicineForm) {
        //校验数据
        Assert.isTrue(Objects.nonNull(storeMedicineForm),"参数为空");
        Assert.isTrue(storeMedicineForm.getMedicineId()!=null,"收藏的草药id为空");
        //获取上下文中的用户信息
        SystemLoginUser systemLoginUser = AuthenticationUtils.getSystemLoginUser();
        Assert.isTrue(Objects.nonNull(systemLoginUser),"上下文中没有用户信息");

        LambdaQueryWrapper<StoreInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreInfo::getMedicineId,storeMedicineForm.getMedicineId());
        wrapper.eq(StoreInfo::getUserId,systemLoginUser.getUserId());
        wrapper.eq(StoreInfo::getDeleted,0);
        Long count = baseMapper.selectCount(wrapper);
        return ResponseDTO.ok(count);
    }

    @Override
    public ResponseDTO<PageDTO<StoreInfo>> storePage(StoreQuery storeQuery) {
        QueryWrapper<StoreInfo> queryWrapper = storeQuery.toQueryWrapper();
        queryWrapper.eq("user_id", AuthenticationUtils.getUserId());
        Page<StoreInfo> storeInfoPage = storeQuery.toPage();
        Page<StoreInfo> page = page(storeInfoPage, queryWrapper);
        return ResponseDTO.ok(new PageDTO<>(page));
    }
}

